﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RyanLam.NET.Core.ITemplates;
using WebComponents.Data;
using System.Data;

namespace RyanLam.NET.Core.Menu
{
    public class Menu : IMenu
    {
        public Menu()
        {
        }
        #region IMenu Members
        /// <summary>
        /// Site menu id.
        /// </summary>
        private int id = 0;
        /// <summary>
        /// Site menu id.
        /// </summary>
        public int ID
        {
            get
            {
                return this.id;
            }
            set
            {
                this.id = value;
            }
        }
        /// <summary>
        /// Menu's parent id.
        /// </summary>
        private int parentID = 0;
        /// <summary>
        /// Menu's parent id.
        /// </summary>
        public int ParentID
        {
            get
            {
                return this.parentID;
            }
            set
            {
                this.parentID = value;
            }
        }
        /// <summary>
        /// Menu's Text
        /// </summary>
        private string text;
        /// <summary>
        /// Menu's Text
        /// </summary>
        public string Text
        {
            get
            {
                return this.text;
            }
            set
            {
                this.text = value;
            }
        }
        /// <summary>
        /// Style sheet class
        /// </summary>
        private string cssClass;
        /// <summary>
        /// Style sheet class
        /// </summary>
        public string CssClass
        {
            get
            {
                return this.cssClass;
            }
            set
            {
                this.cssClass = value;
            }
        }
        /// <summary>
        /// Icon
        /// </summary>
        private string icon;
        /// <summary>
        /// Icon
        /// </summary>
        public string Icon
        {
            get
            {
                return this.icon;
            }
            set
            {
                this.icon = value;
            }
        }
        /// <summary>
        /// Tooltip of menu.
        /// </summary>
        private string tooltip;
        /// <summary>
        /// Tooltip of menu.
        /// </summary>
        public string ToolTip
        {
            get
            {
                return this.tooltip;
            }
            set
            {
                this.tooltip = value;
            }
        }
        /// <summary>
        /// The menu link.
        /// </summary>
        private string navigationUrl;
        /// <summary>
        /// The menu link.
        /// </summary>
        public string NavigationUrl
        {
            get
            {
                return this.navigationUrl;
            }
            set
            {
                this.navigationUrl = value;
            }
        }
        /// <summary>
        /// The menu link's target.
        /// </summary>
        private EnumTarget target;
        /// <summary>
        /// The menu link's target.
        /// </summary>
        public EnumTarget Target
        {
            get
            {
                return this.target;
            }
            set
            {
                this.target = value;
            }
        }
        /// <summary>
        /// Menu force color.
        /// </summary>
        private string forceColor;
        /// <summary>
        /// Menu force color.
        /// </summary>
        public string ForceColor
        {
            get
            {
                return this.forceColor;
            }
            set
            {
                this.forceColor = value;
            }
        }
        /// <summary>
        /// Menu enabled.
        /// </summary>
        private bool enabled;
        /// <summary>
        /// Menu enabled.
        /// </summary>
        public bool Enabled
        {
            get
            {
                return this.enabled;
            }
            set
            {
                this.enabled = value;
            }
        }
        /// <summary>
        /// Menu sorting.
        /// </summary>
        private int sort;
        /// <summary>
        /// Menu sorting.
        /// </summary>
        public int Sort
        {
            get
            {
                return this.sort;
            }
            set
            {
                this.sort = value;
            }
        }
        /// <summary>
        /// Indicate that menu has parent.
        /// </summary>
        public bool HasParent()
        {
            string query = String.Format("SELECT ParentID FROM Menu WHERE ID=@ID");
            List<GetData.ISqlParameter> param = new List<GetData.ISqlParameter>();
            param.Add(new GetData.ISqlParameter { name = "@ID", value = this.ID.ToString() });
            DataTable dt = new DataTable();
            dt = GetData.DataTable(query, param);
            if (int.Parse(dt.Rows[0]["ParentID"].ToString()) == 0)
                return false;
            else
                return true;
        }
        /// <summary>
        /// Indicate that menu has child(s).
        /// </summary>
        public bool HasChild()
        {
            string query = String.Format("SELECT COUNT(*) AS TotalChilds FROM Menu WHERE ParentID=@ID");
            List<GetData.ISqlParameter> param = new List<GetData.ISqlParameter>();
            param.Add(new GetData.ISqlParameter { name = "@ID", value = this.ID.ToString() });
            DataTable dt = new DataTable();
            dt = GetData.DataTable(query, param);
            if (int.Parse(dt.Rows[0]["TotalChilds"].ToString()) == 0)
                return false;
            else
                return true;
        }
        /// <summary>
        /// Indicate that menu allowed for current user.
        /// </summary>
        /// <returns></returns>
        public bool IsAllowed()
        {
            bool _isAllowed = true;
            //foreach (MenuRoles menuRole in Controllers.GetMenuIDsByCurrentUserLogin())
            //{
            //    if (this.ID == menuRole.MenuID)
            //        _isAllowed = false;
            //}
            return _isAllowed;
        }
        /// <summary>
        /// Indicate that menu is current enabled.
        /// </summary>
        /// <returns></returns>
        public bool IsEnabled()
        {
            return this.Enabled;
        }
        #endregion
    }
}
